﻿#include <stdio.h>

//6-1 符号函数
int sign(int x)
{
	if (x < 0)
		return -1;
	else if (x > 0)
		return 1;
	else
		return 0;
}

//6-2 字符金字塔
void CharPyramid(int n, char ch)
{
	for (int i = 1; i <= n; i++) {
		for (int j = 0; j < n - i; j++) {
			printf(" ");
		}

		for (int j = 0; j < i; j++) {
			printf("%c ", ch);
		}

		if (i != n) {
			printf("\n");
		}
	}
}

//6-3 使用函数统计指定数字的个数
int CountDigit(int number, int digit)
{
	int cnt = 0;

	number *= (number >> 31 | 1);

	do {
		if (number % 10 == digit)
			cnt++;
	} while (number /= 10);

	return cnt;
}

//6-4 使用函数求e的近似值
double fact(int n)
{
	double product = 1;
	for (int i = 1; i <= n; i++)
		product *= i;

	return product;
}

//6-5 使用函数求余弦函数的近似值
double funcos(double e, double x)
{
	double item = 1;
	double sum = 1;
	int flag = 1;
	double x2 = x * x;
	for (int i = 1; fabs(item) >= e; i++) {
		flag = -flag;
		item = item * x2 / (4 * i * i - 2 * i);
		sum += flag * item;
	}

	return sum;
}

//6-6 使用函数求素数和
int prime(int p)
{
	if (p <= 1)
		return 0;
	for (int i = 2; i * i <= p; i++)
		if (p % i == 0)
			return 0;
	return 1;
}
int PrimeSum(int m, int n)
{
	int sum = 0;
	for (int i = m; i <= n; i++)
		if (prime(i))
			sum += i;

	return sum;
}

//6-7 使用函数输出指定范围内的Fibonacci数
int fib(int n)
{
	int f1 = 1, f2 = 1;
	for (int i = 3; i <= n; i++) {
		f2 = f1 + f2;
		f1 = f2 - f1;
	}

	return f2;
}
void PrintFN(int m, int n)
{
	int i = 1;
	int f = 0;
	int notfound = 1;
	while (1) {
		f = fib(i++);

		if (f < m)
			continue;

		if (f > n)
			break;

		if (notfound)
			printf("%d", f);
		else
			printf(" %d", f);

		notfound = 0;
	}
	if (notfound)
		printf("No Fibonacci number");
}

//6-8 统计各位数字之和是5的数
int is(int number)
{
	int digits_sum = 0;

	do {
		digits_sum += number % 10;
	} while (number /= 10);

	return digits_sum == 5;
}

void count_sum(int a, int b)
{
	int count = 0;
	int sum = 0;

	while (a <= b) {
		if (is(a)) {
			count++;
			sum += a;
		}
		a++;
	}

	printf("count = %d, sum = %d\n", count, sum);
}

int main()
{
}